import os
import logging
import logging.config
from ament_index_python.packages import get_package_prefix
PACKAGE_NAME ='semantic_nav'


LOGGING_CONFIG_DICT = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'format': '%(asctime)s - %(levelname)s - %(message)s'
        },
        'detailed': {
            'format': '%(asctime)s - %(levelname)s - %(pathname)s:%(lineno)d - %(message)s'
        }
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'level': 'DEBUG',
            'formatter': 'standard',
            'stream': 'ext://sys.stdout'
        },
        'file': {
            'class': 'logging.handlers.RotatingFileHandler',
            'level': 'INFO',
            'formatter': 'detailed',
            'filename': os.path.join(get_package_prefix(PACKAGE_NAME), 'info.log'),
            'maxBytes': 10485760,  # 10MB
            'backupCount': 5,
            'encoding': 'utf8'
        },
        'error_file': {
            'class': 'logging.handlers.RotatingFileHandler',
            'level': 'ERROR',
            'formatter': 'detailed',
            'filename': os.path.join(get_package_prefix(PACKAGE_NAME), 'error.log'),
            'maxBytes': 10485760,  # 10MB
            'backupCount': 5,
            'encoding': 'utf8'
        }
    },
    'loggers': {
        'info.debug': {  
            'handlers': ['console'],
            'level': 'DEBUG',
        },
        'info': {
            'handlers': ['file'],
            'level': 'INFO',
            'propagate': True
        },
        '': { # root logger
            'handlers': ['error_file'],
            'level': 'ERROR',
            'propagate': True
        }
    }
}


def setup_logging_from_dict():
    """use dict to configuring logging"""
    logging.config.dictConfig(LOGGING_CONFIG_DICT)


def get_logger():
    """get logger by name"""
    setup_logging_from_dict()
    return logging.getLogger('info.debug')


if __name__ == '__main__':
    logger = get_logger()
    logger.debug('This is an debug message')
    logger.info('This is an info message')
    logger.error('This is an error message')
